package com.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnCamera;
    private ImageView image;
    private Uri imageUri;
    private static final int REQUEST_CODE_CAMERA_PHOTO = 1;
    private static final int CHOOSE_PHOTO_FROM_ALBUM = 3;
    private Button selectCamera;
    private String TAG = "CameraActivityLog";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_camera);
        initView();
    }

    private void initView() {
        btnCamera = findViewById(R.id.btn_camera);
        image = findViewById(R.id.image);
        selectCamera = findViewById(R.id.select_camera);
        btnCamera.setOnClickListener(this);
        selectCamera.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_camera:
                openCamera();
                break;
            case R.id.select_camera:
                openAlbum();
                break;

            default:
                Toast.makeText(this, "点击", Toast.LENGTH_SHORT).show();
        }
    }

    private void openAlbum() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, CHOOSE_PHOTO_FROM_ALBUM);
    }

    private void openCamera() {
        File file = new File(getExternalCacheDir(), "output_image.png");
        if (file.exists()) {
            file.delete();
        }
        imageUri = FileProvider.getUriForFile(this, "com.base.camera", file);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, REQUEST_CODE_CAMERA_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_CAMERA_PHOTO:
                if (resultCode == RESULT_OK) {
                    image.setImageURI(imageUri);
                }
                break;
            case CHOOSE_PHOTO_FROM_ALBUM:
                if (data != null) {
                    Uri currentPhotoURI = data.getData();
                    image.setImageURI(currentPhotoURI);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

